<script type="text/javascript">
/* Page init setting for User */
KE.show({
    id : 'content',
    cssPath : '<?= Zend_Registry::get('baseDir')?>/Common/Layout/KindEditor.css'
});
var initUrl = "<?= Zend_Registry::get('baseUrl')?>/knowledge/query";
var initParam = "currentpage=1";
var columnName = new Array("knowledgeid", "headline", "creator", "type", "projname");
var operate = "edit,delete";
var currentPage = "1";

$(document).ready(function() {
	fillData(initUrl, initParam, columnName, operate, currentPage);
	markDisabled(Array("edit"));
	$("#type").attr("selectedIndex", 1);
	$("#projectid").val("<?= $_SESSION['currentProjectId']?>");
});

/* Fill data to action panel for Edit */
function goEdit(obj, isEdit) {
	var dataRow = $(obj).parent().parent().children();
	var param = "knowledgeid=" + dataRow.eq(0).text();
	$.post(initUrl, param, function (data) {
		var json = eval(data);
    	var lenJson = json.length - 1;
    	if (lenJson == 1) {
        	$("#knowledgeid").val(json[0]['knowledgeid']);
        	$("#creator").val(json[0]['creator']);
        	$("#type").val(json[0]['type']);
        	$("#projectid").val(json[0]['projectid']);
        	//$("#createdtime").val(json[0]['createdtime']);
        	$("#headline").val(json[0]['headline']);
        	//$("#content").val(json[0]['content']);
        	setKEContent('content', json[0]['content']);
    	}
    	else {
        	$("#msg").text("<?= $this->translate('message.common.nodata')?>");
    	}
	});
	if (isEdit) {
		markEnabled(Array("edit"));
		markDisabled(Array("add", "knowledgeid"));
	}
	else {
		markDisabled(Array("edit"));
		markEnabled(Array("add", "knowledgeid"));
	}
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
	$("#actionPanel").show();
	location.href = "#top";
}

/* Add new data */
function doAdd() {
	var content = replaceAmp(KE.util.getData('content'));
    if (isNull("headline")) {
    	$("#msg").text("<?= $this->translate('message.knowledge.headline.null')?>");
    	return false;
    }
    if ($.trim(content) == "") {
		$("#msg").text("<?= $this->translate('message.knowledge.content.null')?>");
		KE.util.focus('content');
		return false;
	}
	var url = "<?= Zend_Registry::get('baseUrl')?>/knowledge/add";
	var param = "type=" + $.trim($("#type").val()) + 
			"&projectid=" + $.trim($("#projectid").val()) + 
			"&headline=" + $.trim($("#headline").val()) + 
			"&content=" + content;
    $.post(url, param, function (data) {
    	switch (data) {
    	case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.add')?>");
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.add.failed')?>" + "Null");
    		break;
    	default:
    		fillData(initUrl, initParam, columnName, operate, currentPage);
    		$("#knowledgeid").val(data);
    		$("#msg").text("<?= $this->translate('message.common.add.successful')?>" + data);
    	}
    });
}

/* Edit existing data */
function doEdit() {
	var content = replaceAmp(KE.util.getData('content'));
	if (isNull("headline")) {
    	$("#msg").text("<?= $this->translate('message.knowledge.headline.null')?>");
    	return false;
    }
    if ($.trim(content) == "") {
		$("#msg").text("<?= $this->translate('message.knowledge.content.null')?>");
		KE.util.focus('content');
		return false;
	}
	var knowledgeid = $.trim($("#knowledgeid").val());
	var url = "<?= Zend_Registry::get('baseUrl')?>/knowledge/edit";
	var param = "knowledgeid=" + knowledgeid + 
			"&type=" + $.trim($("#type").val()) + 
			"&projectid=" + $.trim($("#projectid").val()) + 
			"&headline=" + $.trim($("#headline").val()) + 
			"&content=" + content;
    $.post(url, param, function (data) {
    	switch (data) {
    	case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.edit')?>");
    		break;
    	case "1":
    		fillEdit(initUrl, "knowledgeid=" + knowledgeid, columnName, knowledgeid);
    		$("#msg").text("<?= $this->translate('message.common.edit.successful')?>" + $("#knowledgeid").val());
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.edit.failed')?>" + $("#knowledgeid").val());
    		break;
        default:
    		$("#msg").text(data);
    	}
	});
}

/* Delete data according to ID */
function doDelete(obj) {
	if(!confirm("<?= $this->translate('message.common.delete.confirm')?>"))
		return false;
	var dataRow = $(obj).parent().parent().children();
	var knowledgeid = $.trim(dataRow.eq(0).text());
	var url = "<?= Zend_Registry::get('baseUrl')?>/knowledge/delete";
	var param = "knowledgeid=" + knowledgeid;
	$.post(url, param, function(data) {
		switch (data) {
		case "no_permission":
    		$("#msg").text("<?= $this->translate('message.common.noperm.delete')?>");
    		break;
    	case "1":
    		$("#msg").text("<?= $this->translate('message.common.delete.successful')?>" + knowledgeid);
			dataRow.remove();
			$("#totalRecord").text($("#totalRecord").text() - 1);
    		break;
    	case "error":
    		$("#msg").text("<?= $this->translate('message.common.delete.failed')?>" + knowledgeid);
    		break;
    	default:
    		$("#msg").text(data);
    	}
	});
}

/* Search data */
function doSearch(isSearch, currentPage) {
	if (isSearch) {
		$("#cacheDiv").removeData("cacheData");
		var cacheArray = new Array();
		cacheArray['knowledgeid'] = $.trim($("#knowledgeid").val());
		cacheArray['creator'] = $.trim($("#creator").val());
		cacheArray['type'] = $.trim($("#type").val());
		cacheArray['projectid'] = $.trim($("#projectid").val());
		cacheArray['headline'] = $.trim($("#headline").val());
		cacheArray['content'] = replaceAmp($.trim(KE.util.getPureData('content')));
		$("#cacheDiv").data("cacheData", cacheArray);
	}
	var cacheData = $("#cacheDiv").data("cacheData");
	if (typeof(cacheData) == "undefined")
		var param = "currentpage=" + currentPage;
	else
		var param = "currentpage=" + currentPage +
				"&knowledgeid=" + cacheData['knowledgeid'] + 
		    	"&creator=" + cacheData['creator'] + 
		    	"&type=" + cacheData['type'] + 
		    	"&projectid=" + cacheData['projectid'] +
		    	"&headline=" + cacheData['headline'] +
		    	"&content=" + cacheData['content'];
	fillData(initUrl, param, columnName, operate, currentPage);
	$("#msg").text("<?= $this->translate('tip.common.action')?>");
}

/* Reset KE content */
function resetKE() {
	setKEContent("content", "");
}
</script>

<br />
<table class="table-action">
<thead>
<tr>
	<a name="top"></a>
	<td colspan="5">
	    <?= $this->translate('common.page.panel')?>:&nbsp;&nbsp;
	    <label id="hideShow" />
	</td>
</tr>
</thead>
<tbody id="actionPanel">
<tr>
	<td width="14%"><?= $this->translate('column.common.id')?>:</td>
	<td width="36%"><input type="text" id="knowledgeid" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="15%"><?= $this->translate('column.knowledge.creator')?>:</td>
	<td width="25%" align="right"><input type="text" id="creator" maxlength="30" value="<?= $_SESSION['userName']?>" class="short" title="<?= $this->translate('tip.common.forsearch')?>" /></td>
	<td width="10%"></td>
</tr>
<tr>
	<td><?= $this->translate('column.knowledge.type')?>:</td>
	<td>
	<select id="type" class="short">
		<option value=""></option>
	    <?php
	        $common = new Common();
	        $knowledgeType = $common->parseDefinition('knowledge-type');
		    foreach ($knowledgeType as $kt) {
                echo '<option value="' , $kt , '">' , $kt , '</option>';
		    }
	    ?>
	</select>
	</td>
	<td><?= $this->translate('column.knowledge.project')?>:</td>
	<td align="right">
	<select id="projectid" class="short">
		<option value=""></option>
	    <?php
		    foreach ($this->project as $project) {
                echo '<option value="' , $project['projectid'] , '">' , $project['projname'] , '</option>';
		    }
	    ?>
	</select>
	</td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.knowledge.headline')?>:</td>
	<td colspan="3"><input type="text" id="headline" maxlength="250" class="long" /></td>
	<td></td>
</tr>
<tr>
	<td><?= $this->translate('column.knowledge.content')?>:</td>
	<td colspan="3"><textarea id="content" class="long"></textarea></td>
	<td></td>
</tr>
</tbody>
<tfoot>
<tr>
	<td><?= $this->translate('common.page.operate')?>:</td>
	<td><span id="actionButton" /></td>
	<td colspan="3">
		<span id="msg" class="message"><?= $this->translate('tip.common.action')?></span>
		<span id="loading" class="loading"><img src="<?= Zend_Registry::get('baseDir')?>/Common/Image/loading.gif" /></span>
	</td>
</tr>
</tfoot>
</table>

<br />
<table class="table-data" id="dataTable">
<thead>
<tr>
    <th width="8%" class="id"><?= $this->translate('column.common.id')?></th>
    <th width="32%"><?= $this->translate('column.knowledge.headline')?></th>
    <th width="15%"><?= $this->translate('column.knowledge.creator')?></th>
    <th width="15%"><?= $this->translate('column.knowledge.type')?></th>
    <th width="15%"><?= $this->translate('column.knowledge.project')?></th>
    <th width="15%"><?= $this->translate('column.common.operate')?></th>
</tr>
</thead>
<tbody id="dataPanel"></tbody>
<tfoot id="footer"></tfoot>
</table>